package com.unipay.mybatis.utils;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

/**
 * @BelongsProject: SSM
 * @Author: cmy
 * @CreateTime: 2023-01-10  16:26
 * @Description: TODO
 */

public class SqlSessionUtil {

    /*
     * @description:
     * @author: cmy
     * @date: 2023/1/10 16:38
     * @param: []
     * @return: org.apache.ibatis.session.SqlSession
     **/
    public static SqlSession getSqlSession() {
        SqlSession sqlSession = null;
        try {
            // 1、获取：核心配置文件的，输入流
            InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
            // 2、获取：sqlSessionFactoryBuilder
            SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
            SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(is);
            sqlSession = sqlSessionFactory.openSession(true);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            System.out.println();
        }
        return sqlSession;
    }
}
